﻿//-------------------------------------------------------------------------
///-----------------JsonMsgMgr---------------------------------------------
///-----------------Json消息解析器-----------------------------------------
///-----------------2022/3/28---by mky.蒙克雨------------------------------
//-------------------------------------------------------------------------
#include "json_mgr.h"
#include "Util/logger.h"

//-------------------------------------------------------------------------------------
mv::JsonMsgMgr::JsonMsgMgr(){

}

//-------------------------------------------------------------------------------------
mv::JsonMsgMgr::~JsonMsgMgr(){

}

//-------------------------------------------------------------------------------------
std::string mv::JsonMsgMgr::GetStringByJson(Json::Value& root) {
	std::lock_guard<std::mutex> locker(m_mutex);
	constexpr bool shouldUseOldWay = true;
	std::string json_file;
	if (shouldUseOldWay) {
		Json::FastWriter writer;
		json_file = writer.write(root);
		//DebugL << json_file;
	}
	else {
		Json::StreamWriterBuilder builder;
		json_file = Json::writeString(builder, root);
		//DebugL << json_file;
	}
	return std::move(json_file);
}

//-------------------------------------------------------------------------------------
Json::Value mv::JsonMsgMgr::GetJsonByString(const std::string& rawJson) {
	std::lock_guard<std::mutex> locker(m_mutex);
	const auto rawJsonLength = static_cast<int>(rawJson.length());
	constexpr bool shouldUseOldWay = true;
	JSONCPP_STRING err;
	Json::Value root;
	if (shouldUseOldWay) {
		Json::Reader reader;
		reader.parse(rawJson, root);
	}
	else {
		Json::CharReaderBuilder builder;
		const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
		if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
			&err)) {
			DebugL << "error";
		}
	}
	return std::move(root);
}

//-------------------------------------------------------------------------------------
Json::Value 
mv::JsonMsgMgr::GetJsonMsgHead(
	const std::string& method,
	const int& code,
	const std::string& message,
	const std::string& requestId,
	const std::string& version,
	const std::string& clientId,
	const std::string& deviceName) 	
{
	Json::Value root;
	if (requestId !="")
		root["requestId"] = requestId;
	if (version != "")
		root["version"] = version;
	if (method != "")
		root["method"] = method;
	if (clientId != "")
		root["clientId"] = clientId;
	if (code)
		root["code"] = code;
	if (message != "")
		root["message"] = message;
	if (deviceName != "")
		root["deviceName"] = deviceName;
	return std::move(root);
}

//-------------------------------------------------------------------------------------
std::string 
mv::JsonMsgMgr::GetJsonMsg(const std::string& message) {
	Json::Value value = GetJsonByString(message);
	return GetStringByJson(value);
}

//-------------------------------------------------------------------------------------
std::string mv::JsonMsgMgr::GetJsonMsg(const std::string& method, const int& code, const std::string& message,const std::string& requestId,bool modle) {
	Json::Value root;
	root["method"] = method;
	if (modle) {
		root["code"] = code;
	}
	else {
		root["code"] = std::to_string(code);
	}
	root["message"] = message;
	root["requestId"] = requestId;
	return GetStringByJson(root);
}

//-------------------------------------------------------------------------------------
std::string mv::JsonMsgMgr::GetJsonMsg(const std::string& method, const int& code, const std::string& message, bool modle) {
	Json::Value root;
	root["method"] = method;
	if (modle) {
		root["code"] = code;
	}
	else {
		root["code"] = std::to_string(code);
	}

	root["message"] = message;
	return GetStringByJson(root);
}
//-------------------------------------------------------------------------------------
std::string
mv::JsonMsgMgr::GetJsonMsgBody(Json::Value& root)
{
	return std::move(GetStringByJson(root));
}
//-------------------------------------------------------------------------------------
std::string 
mv::JsonMsgMgr::GetJsonMsgBody(Json::Value& root, const Json::Value& params)
{
	root["params"] = params;
	return std::move(GetStringByJson(root));
}

//-------------------------------------------------------------------------------------
std::string mv::JsonMsgMgr::GetJsonMsg(Json::Value& task, const std::string& code, const std::string& message) {
	//InfoL << "上报状态：" << code << " message:" << message;
	Json::Value root;
	try{
		root["requestId"] = task.get("requestId", "").asString();
		root["version"] = task.get("version", "").asString();
		root["method"] = task.get("method", "").asString();
		root["code"] = code;
		root["message"] = message;
		root["deviceName"] = task["params"].get("deviceName", "").asString();
	}
	catch(const std::exception& e){
		ErrorL << "task.get 异常：" << e.what() << '\n';
	}


	return std::move(GetStringByJson(root));
}
//-------------------------------------------------------------------------------------